﻿Option Strict On
Option Explicit On 

Imports System.Windows.Forms
Imports SIS.controls.button



''' -----------------------------------------------------------------------------
''' Project	 : IPDataControl
''' Class	 : Core.IPDataControl.CDataEntryFormButtonsStateControl
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Để control trạng thái các button trên form DE
''' </summary>
''' <remarks>
''' Các nút được control gồm có
''' <list type="bullet">
''' <item>1 -  nút previous</item>
''' <item>2 -  nút next</item>
''' <item>3 -  nút sửa </item>
''' <item>4 -  nút tìm kiếm</item>
''' <item>5 -  nút thêm</item>
''' <item>6 - nút xóa </item>
''' <item>7 -  nút lưu</item>
''' <item>8 - nút cancel</item>
''' </list>
''' </remarks>
''' <history>
''' 	[csung]	3/7/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CDataEntryFormButtonsStateControl

#Region "PUBLIC"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Add các button vào để điều khiển
    ''' </summary>
    ''' <param name="i_en_function_of_button"></param>
    ''' <param name="i_btn"></param>
    ''' <example>
    ''' <code language ="VB.NET">
    ''' 
    ''' </code>
    ''' </example>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub add_de_button(ByVal i_en_function_of_button As IPDEButtonEnum, _
                            ByVal i_btn As SiSButton)
        Select Case i_en_function_of_button
            Case IPDEButtonEnum.Cancel
                m_btn_cancel = i_btn
            Case IPDEButtonEnum.Delete
                m_btn_delete = i_btn
            Case IPDEButtonEnum.Insert
                m_btn_insert = i_btn
            Case IPDEButtonEnum.MoveNext
                m_btn_next = i_btn
            Case IPDEButtonEnum.MovePrevious
                m_btn_previous = i_btn
            Case IPDEButtonEnum.Save
                m_btn_save = i_btn
            Case IPDEButtonEnum.Search
                m_btn_search = i_btn
            Case IPDEButtonEnum.Update
                m_btn_update = i_btn
        End Select
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Bắt đầu update, gọi khi user ấn nút update
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub start_update()
        disable_button(m_btn_delete)
        disable_button(m_btn_next)
        disable_button(m_btn_previous)
        disable_button(m_btn_search)
        disable_button(m_btn_insert)
        disable_functional_buttons()
        disable_button(m_btn_update)

        enable_button(m_btn_cancel)
        enable_button(m_btn_save)


        m_state_of_form_de = IPFormDEState.DE_UpdateState
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Bắt đầu insert, gọi khi user ấn nút tạo mới
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub start_insert_new()
        disable_button(m_btn_delete)
        disable_button(m_btn_next)
        disable_button(m_btn_previous)
        disable_button(m_btn_search)
        disable_button(m_btn_update)
        disable_functional_buttons()
        disable_button(m_btn_insert)

        enable_button(m_btn_cancel)
        enable_button(m_btn_save)


        m_state_of_form_de = IPFormDEState.DE_InsertState

    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Quay lại mode view. Thường được gọi khi cancel hoặc commit thành công
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overridable Sub go_to_view_mode()
        enable_button(m_btn_delete)
        enable_button(m_btn_next)
        enable_button(m_btn_previous)
        enable_button(m_btn_search)
        enable_button(m_btn_update)
        enable_button(m_btn_insert)

        disable_button(m_btn_cancel)
        disable_button(m_btn_save)

        'enable functional buttons
        Dim v_i_index As Integer
        For v_i_index = 0 To m_arr_functinal_button.Count - 1
            Dim v_btn_functional As SiSButton
            v_btn_functional = CType(m_arr_functinal_button(v_i_index), SiSButton)
            enable_button(v_btn_functional)
        Next
        m_state_of_form_de = IPFormDEState.DE_ViewState
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' State của form DE là gì
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_state_of_ipde_form() As IPFormDEState
        Return m_state_of_form_de
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Add thêm nút (ngòai các nút thông thường)
    ''' </summary>
    ''' <param name="i_btn_functional"></param>
    ''' <remarks>
    ''' <list type="bullet">
    ''' <item>Các nút này thường làm một function gì đó ngòai những button đã liệt kê khi tạo đối tượng này. </item>
    ''' <item>Chú ý: sẽ bị disable và enable như các nút next, previous </item>
    ''' </list>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub add_functional_button(ByVal i_btn_functional As SiSButton)
        m_arr_functinal_button.Add(i_btn_functional)
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Add event handler cho 1 sự kiện của form DE
    ''' </summary>
    ''' <param name="i_en_function_of_button"></param>
    ''' <param name="i_btn"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub add_de_click_event_handler(ByVal i_en_function_of_button As IPDEButtonEnum, _
                                    ByVal i_eventhandler As EventHandler)
        Select Case i_en_function_of_button
            Case IPDEButtonEnum.Cancel
                Me.add_click_handler(m_btn_cancel, i_eventhandler)
            Case IPDEButtonEnum.Delete
                Me.add_click_handler(m_btn_delete, i_eventhandler)
            Case IPDEButtonEnum.Insert
                Me.add_click_handler(m_btn_insert, i_eventhandler)
            Case IPDEButtonEnum.MoveNext
                Me.add_click_handler(m_btn_next, i_eventhandler)
            Case IPDEButtonEnum.MovePrevious
                Me.add_click_handler(m_btn_previous, i_eventhandler)
            Case IPDEButtonEnum.Save
                Me.add_click_handler(m_btn_save, i_eventhandler)
            Case IPDEButtonEnum.Search
                Me.add_click_handler(m_btn_search, i_eventhandler)
            Case IPDEButtonEnum.Update
                Me.add_click_handler(m_btn_update, i_eventhandler)
        End Select

    End Sub
#End Region

#Region "Members"
        Protected WithEvents m_btn_save As SiSButton
        Protected WithEvents m_btn_delete As SiSButton
        Protected WithEvents m_btn_search As SiSButton
        Protected WithEvents m_btn_update As SiSButton
        Protected WithEvents m_btn_insert As SiSButton
        Protected WithEvents m_btn_next As SiSButton
        Protected WithEvents m_btn_previous As SiSButton
        Protected WithEvents m_btn_cancel As SiSButton

        Private m_state_of_form_de As IPFormDEState = IPFormDEState.DE_ViewState
        Private m_arr_functinal_button As New ArrayList

#End Region

#Region "PRIVATES"
    Private Sub disable_button(ByVal i_button As SiSButton)
        If i_button Is Nothing Then Exit Sub
        i_button.Enabled = False
    End Sub

    Private Sub enable_button(ByVal i_button As SiSButton)
        If i_button Is Nothing Then Exit Sub
        i_button.Enabled = True
    End Sub


    Private Sub disable_functional_buttons()
        Dim v_i_index As Integer
        For v_i_index = 0 To m_arr_functinal_button.Count - 1
            Dim v_btn_functional As SiSButton
            v_btn_functional = CType(m_arr_functinal_button(v_i_index), SiSButton)
            disable_button(v_btn_functional)
        Next
    End Sub

    Private Sub add_click_handler(ByVal i_btn As SiSButton, ByVal i_handler As EventHandler)
        If Not i_btn Is Nothing Then
            AddHandler i_btn.Click, i_handler
        End If
    End Sub
#End Region



End Class


